﻿using System.Collections.Generic;
using System.IO;
using System.Xml.Serialization;

namespace Halk.Config
{
    public class ContentManage
    {
        private static ContentManage _contentManage;

        public static ContentManage Instace()
        {
            return _contentManage ?? (_contentManage = new ContentManage());
        }

        private ContentInfo _content;

        public ContentInfo Content
        {
            get { return _content; }
        }

        private static readonly object ObjLock = new object();

        /// <summary>
        /// 初始化配置文件
        /// </summary>
        public void Load()
        {
            lock (ObjLock)
            {
                _content = new ContentInfo();
                using (FileStream stream = File.Open(@"conf\content.xml", FileMode.Open))
                {
                    var xmlserializer = new XmlSerializer(typeof(ContentInfo));
                    _content = xmlserializer.Deserialize(stream) as ContentInfo;
                    stream.Close();
                    stream.Dispose();
                }
            }
        }

        private Dictionary<int, string> _lagerSortNames = new Dictionary<int, string>();

        private Dictionary<int, string> _lagerSortIDs = new Dictionary<int, string>();

        //private Dictionary<string, string> _smallSortNames = new Dictionary<string, string>();

        //private Dictionary<string, int> _smallSortIDs = new Dictionary<string, int>();

        public string GetLagerSortName(int sortid)
        {
            if (_lagerSortNames == null || _lagerSortNames.Count == 0)
            {
                LoadSubSort(ref _lagerSortNames, ref _lagerSortIDs);
            }
            if (_lagerSortNames.ContainsKey(sortid))
            {
                return _lagerSortNames[sortid];
            }
            return _content.DefaultLagerSort;
        }

        public int GetLagerSortId(string sortName)
        {
            if (_lagerSortNames == null || _lagerSortNames.Count == 0)
            {
                LoadSubSort(ref _lagerSortIDs, ref _lagerSortNames);
            }
            foreach (KeyValuePair<int, string> sort in _lagerSortIDs)
            {
                if (sort.Value.Contains(sortName))
                {
                    return sort.Key;
                }
            }
            return _content.DefaultLagerSortID;
        }

        //public string GetSmallSortName(string sortName)
        //{
        //    if (_smallSortNames == null || _smallSortNames.Count == 0)
        //    {
        //        LoadSubSort(_content.SmallSortCorresponding, ref _smallSortNames, ref _smallSortIDs);
        //    }
        //    if (_smallSortNames.ContainsKey(sortName))
        //    {
        //        return _smallSortNames[sortName];
        //    }
        //    return _content.DefaultSmallSort;
        //}

        //public int GetSmallSortID(string sortName)
        //{
        //    if (_smallSortNames == null || _smallSortNames.Count == 0)
        //    {
        //        LoadSubSort(_content.SmallSortCorresponding, ref _smallSortNames, ref _smallSortIDs);
        //    }
        //    if (_smallSortIDs.ContainsKey(sortName))
        //    {
        //        return _smallSortIDs[sortName];
        //    }
        //    return _content.DefaultSmallSortID;
        //}

        private void LoadSubSort(ref Dictionary<int, string> sortIDs, ref Dictionary<int, string> sortNames)
        {
            string[] arrLagerSorts = Content.LagerSortCorresponding.Split('♂');
            foreach (var arrSort in arrLagerSorts)
            {
                string[] arrId = arrSort.Split('|');
                if (arrId.Length > 1)
                {
                    int lagerId = int.Parse(arrId[0]);
                    sortIDs[lagerId] = arrId[1];
                    string[] arrNames = arrId[1].Split('=');
                    sortNames[lagerId] = arrNames[0];
                }
            }
        }

        private readonly Dictionary<string, string> _smartReplaceString = new Dictionary<string, string>();

        private void LoadSmartReplaceList()
        {
            var arrs = Instace().Content.SmartMatchString.Split('|');
            foreach (var arr in arrs)
            {
                var strs = arr.Split('♂');
                if (strs.Length > 1)
                {
                    _smartReplaceString[strs[0]] = strs[1];
                }
            }
        }

        public Dictionary<string, string> GetSmartReplaceString()
        {
            if (_smartReplaceString == null || _smartReplaceString.Count == 0)
            {
                LoadSmartReplaceList();
            }
            return _smartReplaceString;
        }
    }
}